package com.ouyu.mvc.jdbc;


import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ouyu
 * Date: 2018-10-26
 * Time: 9:17
 */
public class ConnectionJdbc {
    private static String url="jdbc:mysql://127.0.0.1:3306/yajin?useUnicode=true&amp;characterEncoding=utf-8";

    private static String user="root";

    private static String password="root";

    private static Connection connection=null;
    private static PreparedStatement  statement = null;
    private static ResultSet rs=null;

    public static Connection getConnection(){
        if(connection!=null){
            return connection;
        }
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url,user,password);
            return connection;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T writeSql(String sql,Class cl,Type type){
        try {
            statement = getConnection().prepareStatement(sql);
             rs=statement.executeQuery();
            List<T> list = new ArrayList<>();

            //拿到返回类型的泛型
            System.out.println( type.getTypeName());
            if(type instanceof ParameterizedType){
                ParameterizedType pt = (ParameterizedType) type;
                //得到泛型里的class类型对象
                cl = (Class<?>)pt.getActualTypeArguments()[0];
            }

            while(rs.next()){
                Object ob = cl.newInstance();
                Field[] fields= cl.getDeclaredFields();

                    for(Field field:fields){

                     Class c= field.getType();
                    String str= c.getName();

                        switch (str){
                            case "int":
                                field.setAccessible(true);
                                System.out.println(field.getName());
                                field.set(ob,rs.getInt(field.getName()));
                                break;
                            case "java.lang.String":
                                field.setAccessible(true);
                                field.set(ob,rs.getString(field.getName()));
                                break;

                            case "java.util.Date":
                                field.setAccessible(true);
                                field.set(ob,rs.getDate(field.getName()));
                                break;
                        }

                    }
                 list.add((T) ob);
                /*      System.out.println(rs.getString("name")+" "
                                            +rs.getString("phone"));*/
             }
             return (T) list;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
            return null;
    }
}
